/****************************************************************************
**
** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtWebSockets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "echoserver.h"
#include "QtWebSockets/qwebsocketserver.h"
#include "QtWebSockets/qwebsocket.h"
#include <QtCore/QDebug>
#include <QNetworkInterface>

#include "robotmanager.h"
#include "robotcontrol.h"
#include "programengine.h"
#include "powermanager.h"
extern PowerManager *p_powerManger;
UserInfo userInfo ;
//EchoServer webobj;
//EchoServer webctl;hualei no use


EchoServer::EchoServer(  QObject *parent) :
    QThread(parent)
{
    versionInfo.componentName="WebServer";
    versionInfo.componentVersion="V1.0";
    versionInfo.publishDate="2018.1.3";

    m_pWebSocketServer=NULL;
    stateflag=0;
    isRun=false;
    connect( this, &EchoServer::signsl_listen, this,&EchoServer::socketConnected  );
}

int EchoServer::readVersion(std::vector <ComponentVersion> &componentVersionList)
{
    componentVersionList.push_back(versionInfo);
    return 1;
}

void EchoServer::run(){
    if( !m_pWebSocketServer ){
        m_pWebSocketServer = new QWebSocketServer ( QStringLiteral("TcrWebServer"),  QWebSocketServer::NonSecureMode ) ;
        connect( m_pWebSocketServer, &QWebSocketServer::newConnection, this, &EchoServer::onNewConnection );
        connect( m_pWebSocketServer, &QWebSocketServer::closed, this, &EchoServer::closed );
    }
    isRun=true;
    exec();
}

EchoServer::~EchoServer()
{
    closed();
}

void EchoServer::onNewConnection()
{
    qtlock.lock();
    QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
    connect(pSocket, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage);
    connect(pSocket, &QWebSocket::binaryMessageReceived, this, &EchoServer::processBinaryMessage);
    connect(pSocket, &QWebSocket::disconnected, this, &EchoServer::socketDisconnected);
    m_clients << pSocket;
    qtlock.unlock();
    qDebug()<<"new connect !!";
}

void EchoServer::processTextMessage(QString message)
{
    if( message.size()<9 )
        return;

    if( qtlock.tryLock() ){
        QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
        if (pClient) {
            cmdAnalysis( pClient, message );
           // writeWebCmd( pClient, message );
          //  emit signal_readWebCmd( pClient, message);
        }
        qtlock.unlock();
    }
}

void EchoServer::processBinaryMessage(QByteArray message)
{
     qDebug() << "Binary Message received:" <<message.size() ;
}

void EchoServer::socketConnect(QString ip, int port )
{
    emit signsl_listen(ip, port );
}

void EchoServer::socketDisconnected()
{
    QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
    qDebug() << "socketDisconnected:" << pClient;
    if (pClient) {

        if( qtlock.tryLock() ){
            for(int i=0;i<m_clients.size();i++){
                if( pClient == m_clients.at(i) ){
                    disconnect(pClient, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage);
                    disconnect(pClient, &QWebSocket::binaryMessageReceived, this, &EchoServer::processBinaryMessage);
                    m_clients.removeAt( i );
                    break;
                }
            }
            pClient->deleteLater();
            qtlock.unlock();
        }
    }
}

void EchoServer::closed(){
    qtlock.lock() ;
    if( m_pWebSocketServer ){
        m_pWebSocketServer->close();
    }
    qDeleteAll(m_clients.begin(), m_clients.end());
    qtlock.unlock();
}

void EchoServer::socketConnected( QString ip,  int port )
{
    if( !m_pWebSocketServer )
        return ;

#if 1
    quint32 nIPV4;
    QStringList IPV6List("");
    QList<QHostAddress> list = QNetworkInterface::allAddresses();
    foreach (QHostAddress address, list) {
        // 主机地址为空
        if (address.isNull())
            continue;
        qDebug() << "********************";
        QAbstractSocket::NetworkLayerProtocol nProtocol = address.protocol();
        QString strScopeId = address.scopeId();
        QString strAddress = address.toString();
        bool bLoopback = address.isLoopback();

        // 如果是IPv4
        if (nProtocol == QAbstractSocket::IPv4Protocol) {
            // bool bOk = false;
            nIPV4 = address.toIPv4Address( );
            //  if (bOk)
            qDebug() << "IPV4 : " << nIPV4;//<< address
        }
        // 如果是IPv6
        else if (nProtocol == QAbstractSocket::IPv6Protocol) {
            Q_IPV6ADDR IPV6 = address.toIPv6Address();
            for (int i = 0; i < 16; ++i) {
                quint8 nC = IPV6[i];
                IPV6List << QString::number(nC);
            }
            qDebug() << "IPV6 : " << IPV6List.join(" ");
        }

//        qDebug() << "Protocol : " << nProtocol;
//        qDebug() << "ScopeId : " << strScopeId;
//        qDebug() << "Address : " << strAddress;
//        qDebug() << "IsLoopback  : " << bLoopback;
    }
    if (m_pWebSocketServer->listen( QHostAddress(nIPV4)  , port))
    {
        qDebug()<<" webserver===list ok !"<< nIPV4 <<port  ;
#else
    if (m_pWebSocketServer->listen( QHostAddress(ip)  , port))
    {
        qDebug() << "TcrWebServer list ok !"<< ip <<port  ;
#endif
    }else{
        qDebug()<<" webserver=== list failed !"<< ip <<port  ;
    }
}

void EchoServer::writeWebCmd( QObject *id,  QString cmd  )
{
    if(id){
        QWebSocket *pClient = qobject_cast<QWebSocket *>( id );
        if( pClient ) {
            pClient->sendTextMessage( cmd  );
        }
    }
}

void EchoServer::openfile_return(int robId, int ret ){
    qDebug()<<"====openfile_return===="<<stateflag;
    if( ret ) {
        stateflag = eFileOpenOk;
    }else{
        stateflag = eFileOpenFailed;
      //  curRobotId = rob->robotInfo.robotId;
    }
}

int EchoServer::cmdAnalysis( QWebSocket  *id , QString message)
{
    int returnFlag=0;
    if( !p_powerManger  )
        return 0;

    QChar strCrc=message.at(8);
    if( strCrc != ':' ){
        qDebug()<<"cmd failed" << message << strCrc  ;
        return 0;
    }
    QString strCmd=message.left(8);
    QString strData=message.mid(9);
    QStringList dataList = strData.split(";", QString::SkipEmptyParts );
    if( strCmd != "getState" &&  strCmd != "nowPoint"  )
        qDebug()<<"cmd ok " << strCmd << strCrc << strData ;

    if( strCmd=="getState" )
    {    // 安全开关，使能否
        QString msg="getState:";

        switch ( stateflag ) {
        case eFileOpenOk:
            qDebug()<< " =========writeWebCmd(openFile_:true ;";
            writeWebCmd( id,  "openFile_:true" );
            stateflag=0;
            break;
        case eFileOpenFailed:
            qDebug()<< " ===========writeWebCmd(openFile_:false ;";
            writeWebCmd( id,  "openFile_:false" );
            stateflag=0;
            break;
        default:
            break;
        }
#ifndef D_USE_MOTIONSERVER
        if( robCtl.isServoOnRobot( p_powerManger->robotId ) ){
            msg.append( "on;");
        }else{
            msg.append( "off;");
        }
#else
        if( p_powerManger->robotManager->robotsMap.value(p_powerManger->robotId)->threadMotionEngine->mcControllerInterpreter->servoOn() ){
            msg.append( "on;");
        }else{
            msg.append( "off;");
        }

#endif


        RobotEngine*rob = p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob ){
            //=============
            int mcRunStatus = rob->threadMotionEngine->mcControllerInterpreter->getStatus();
            int proRunStatus = rob->programEngine->engineStatus;
            if(   ENUM_RUN == proRunStatus || ENUM_RUN == mcRunStatus ) { //RUN
                 msg.append( "run;");
            }else if( ENUM_CEASE == proRunStatus ){//暂停
                msg.append( "pause;");
            }else{
                 msg.append( "stop;");
            }
            int runModel = rob->programEngine->runModel;
            if(ENUM_CONTROL_T1 == runModel){
                msg.append( "T1;");
            }else if(ENUM_CONTROL_T2 == runModel){
                msg.append( "T2;");
            }else if(ENUM_CONTROL_AUTO == runModel){
                msg.append( "A;");
            }else if(ENUM_CONTROL_EXT == runModel){
                 msg.append( "E;");
            }

            if( ENUM_JOINT== rob->threadMotionEngine->mcControllerInterpreter->getJogCoordinate() )
            {
                msg.append( "J;");
            }else if( ENUM_WORLD== rob->threadMotionEngine->mcControllerInterpreter->getJogCoordinate() ){
                msg.append( "W;");
            }else if( ENUM_TOOL== rob->threadMotionEngine->mcControllerInterpreter->getJogCoordinate() ){
                msg.append( "T;");
            }else if( ENUM_USER== rob->threadMotionEngine->mcControllerInterpreter->getJogCoordinate() ){
                msg.append( "U;");
            }

            if(  rob->programEngine->moveType == 0 ){
                msg.append( "step;");
            }else{
                msg.append( "loop;");
            }

            msg.append( QString::number(  (float)(100 *  rob->threadMotionEngine->mcControllerInterpreter->manualRatio))  ) ;
            msg.append( ";" ) ;
            msg.append( QString::number(  (float)(100 * rob->threadMotionEngine->mcControllerInterpreter ->autoRatio) ) ) ;
            msg.append( ";" ) ;    // 自动速度倍率
        }
        writeWebCmd( id,  msg );
    }else  if( strCmd=="nowPoint" ){
        QString strdata;
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob ){
            UpdateDataLv2  updateDataLv2;
             rob->threadMotionEngine->mcControllerInterpreter->updateControllerInfoLevel2( updateDataLv2 );
            for(int i=0;i<updateDataLv2.currentPositionWorld.positionValue.size(); i++ ){
                strdata.append( QString::number( updateDataLv2.currentPositionWorld.positionValue.at(i) ));
                strdata.append( ";" );
            }
            for(int i=0;i<updateDataLv2.currentPositionJoint.positionValue.size(); i++ ){
                strdata.append( QString::number( updateDataLv2.currentPositionJoint.positionValue.at(i) ));
                strdata.append( ";" );
            }
        }
        writeWebCmd( id, "nowPoint:" + strdata  );

    }else  if( strCmd=="jogDog__" ){
        //  jogRunDog( NULL,userInfo, returnFlag );
        p_powerManger->feedDog();
    }else  if( strCmd=="run_____" ){
        p_powerManger->programRun( userInfo,   returnFlag ) ;
    }else  if( strCmd=="stop____" ){
        p_powerManger->programReset( userInfo,  returnFlag ) ;
    }else  if( strCmd=="pause___" ){
        p_powerManger->programCease( userInfo,  returnFlag ) ;
    }else  if( strCmd=="setServo" ){
        int nServOffOn = ENUM_SERVO_OFF;
        if( strData.indexOf("on")>=0 ){
            nServOffOn=ENUM_SERVO_ON;
        }else{
            nServOffOn=ENUM_SERVO_OFF;
        }
        p_powerManger->setManualServon( userInfo, nServOffOn, returnFlag ) ;
    }else  if( strCmd=="getPoint" ){
        int index = strData.toInt();
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob ){
            QString senddata="getPoint:";
            std::vector<float> positionValue=  rob->programEngine->fData.getPointProList.at( index ).positionValue;
            if( rob->programEngine->fData.getPointProList.at( index ).jointFlag==1  ){
                senddata.append( "W" );
            }else{
                senddata.append( "J" );
            }
            for(int i=0;i<positionValue.size();i++){
                senddata.append( ";" );
                senddata.append( QString::number( positionValue.at(i)) );
            }
            senddata.append( ";" );
            writeWebCmd( id,  senddata );
        }
    }else  if( strCmd=="setPoint" ){
        RobotEngine*rob = p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob )
        {
            if( dataList.size()>=8 )
            {
                int axisCount = dataList.size() -2-6;
                int index=dataList.at(0).toInt()-1;

                int p_type = 0;
                if( dataList.at(1) == "J"){
                    p_type=0;
                }else{
                    p_type=1;
                }
                if( index <0 )
                    return 0;

                int oldSize= rob->programEngine->fData.getPointProList.size() ;
                qDebug()<<"axisCount="<< axisCount << index <<oldSize;
                if( index+1 >  oldSize ){
                    rob->programEngine->fData.getPointProList.resize( index+1 );
                    if( oldSize<1 )
                        oldSize=1;

                    for(int i=oldSize-1;i< index+1; i++){
                        rob->programEngine->fData.getPointProList[ i ].pointNum = 0 ;
                        rob->programEngine->fData.getPointProList[ i ].groupNum = 0 ;
                        rob->programEngine->fData.getPointProList[ i ].ufValue = rob->threadMotionEngine->mcControllerInterpreter->getUserIndex();
                        rob->programEngine->fData.getPointProList[ i ].utValue = rob->threadMotionEngine->mcControllerInterpreter->getToolIndex();
                        rob->programEngine->fData.getPointProList[ i ].recordStatus = 0 ;
                        rob->programEngine->fData.getPointProList[ i ].configString ="";
                        rob->programEngine->fData.getPointProList[ i ].configInt.resize( axisCount );
                        rob->programEngine->fData.getPointProList[ i ].positionValue.resize( axisCount );
                        rob->programEngine->fData.getPointProList[ i ].extPositionValue.resize( 0 );

                        if(  dataList.at(1).indexOf( "J")>=0 ){
                            rob->programEngine->fData.getPointProList[ i ].jointFlag = 0 ;
                            for(int taxis=0;taxis<axisCount;taxis++ ){
                        //        qDebug()<<" jjjjjjj=="<< dataList.at( taxis+2+6 )<<dataList.at( taxis+2+6 ).toFloat();
                                rob->programEngine->fData.getPointProList[ i ].positionValue[taxis]=dataList.at( taxis+2+6 ).toFloat();
                            }
                        }else{
                            rob->programEngine->fData.getPointProList[ i ].jointFlag = 1 ;
                            for(int taxis=0;taxis<axisCount;taxis++ ){
                                rob->programEngine->fData.getPointProList[ i ].positionValue[taxis]=dataList.at( taxis+2 ).toFloat();
                            }
                        }
                    }
                }else{
                    rob->programEngine->fData.getPointProList[ index ].pointNum = 0 ;
                    rob->programEngine->fData.getPointProList[ index ].groupNum = 0 ;
                    rob->programEngine->fData.getPointProList[ index ].ufValue = rob->threadMotionEngine->mcControllerInterpreter->getUserIndex();
                    rob->programEngine->fData.getPointProList[ index ].utValue = rob->threadMotionEngine->mcControllerInterpreter->getToolIndex();
                    rob->programEngine->fData.getPointProList[ index ].recordStatus = 0 ;
                    rob->programEngine->fData.getPointProList[ index ].configString ="";
                    rob->programEngine->fData.getPointProList[ index ].configInt.resize( axisCount );
                    rob->programEngine->fData.getPointProList[ index ].positionValue.resize( axisCount );
                    rob->programEngine->fData.getPointProList[ index ].extPositionValue.resize( 0 );

                    qDebug()<<"axisCount="<< axisCount << index <<index;
                    if(  dataList.at(1).indexOf( "J")>=0 ){
                        rob->programEngine->fData.getPointProList[ index ].jointFlag = 0 ;
                        for(int taxis=0;taxis<axisCount;taxis++ ){
                         //   qDebug()<<" jjjjjjj=="<< dataList.at( taxis+2+6 )<<dataList.at( taxis+2+6 ).toFloat();
                            rob->programEngine->fData.getPointProList[ index ].positionValue[taxis]=dataList.at( taxis+2+6 ).toFloat();
                        }
                    }else{
                        rob->programEngine->fData.getPointProList[ index ].jointFlag = 1 ;
                        for(int taxis=0;taxis<axisCount;taxis++ ){
                            rob->programEngine->fData.getPointProList[ index ].positionValue[taxis]=dataList.at( taxis+2 ).toFloat();
                        }
                    }
                }
            }
        }
    }else  if( strCmd=="movPoint" ){

        bool isOK=false;
       // p_powerManger->moveToPoint(  userInfo,  returnFlag ) ;
        int axisNumber=dataList.at(0).toInt(&isOK)-1;
      //  int direction=0;
        if( isOK && dataList.size()==2 && axisNumber>=0 )
        {
            RobotEngine* rob= p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId );
            if(!rob)
                return 0;
            if( rob->programEngine->programSet.size()<0 )
                return 0;

            int pVal= axisNumber;
            int returnFlag=0;
            MoveParameter tmpMovePara;

            if( dataList.at(1) == "L" ){
                tmpMovePara.moveType = ENUM_COMMAND_LINE;
            }else{
                tmpMovePara.moveType = ENUM_COMMAND_JOINT_SPLINE;
            }
            tmpMovePara.velocityUnit=ENUM_RATIO; //
        //    tmpMovePara.velocity = ui->spinBox_manual_speed->value();//
            tmpMovePara.velocity = 100.0;//
            tmpMovePara.pointType = ENUM_NORMAL_POINT;

            tmpMovePara.index = pVal ;
            tmpMovePara.cntRatio = 0;
            tmpMovePara.acc = 100;//20
            tmpMovePara.lineNumber =0;
            if(  pVal< p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId )->programEngine->pointData->pointProList.size()  )
            {
                tmpMovePara. targetPoint = p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId )->programEngine->pointData->pointProList[pVal] ;//hmiAPI_update20161107.h
                p_powerManger-> moveToPoint( userInfo , tmpMovePara, returnFlag  );
            }

            p_powerManger-> moveToPoint( userInfo , tmpMovePara, returnFlag  );
        }

    }else  if( strCmd=="openFile" ){
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob ){
                rob->openFile( strData );
        }
    }else  if( strCmd=="newFile_" ){
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob )
        {
            //copy
             QString filename=  dataList.at(0);
             if( filename .indexOf(".txt")>=0)
             {
                 QString filepath = QString::fromStdString(rob->robotInfo.robotFileAddress)+"program/"+filename;
                 QString tfile = QString::fromStdString(rob->robotInfo.robotFileAddress)+"program/tmpfile.txt";
                 qDebug()<< filename<<  tfile <<filepath;
                 QFile::copy(  tfile,  filepath );

                 if( !QFile::exists( filepath ) )
                 {
                     qDebug()<< " !QFile::exists( filepath ) ";
                     return 0;
                 }
                 dataList.removeAt(0);
                 rob->openFile( filename );
                 if( dataList.size()>0 )
                 {
                     rob->programEngine->fData.cmdStrWriteList = dataList;
                     rob->programEngine->fData.writeProgramFile( filename );
                     rob->openFile( filename );
                 }
             }
        }
    }else  if( strCmd=="saveFile" ){
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob ){

          qDebug()<<"fffffffwffff= getPointProList.size="<<   rob->programEngine->fData. getPointProList.size();
            for(int kk=0; kk< rob->programEngine->fData. getPointProList.size(); kk++ ){
                qDebug()<<"pos:[ "<<kk <<" ]=";
                    for(int jj=0; jj< rob->programEngine->fData. getPointProList[jj].positionValue.size(); jj++ ){
                        qDebug()<<jj<<  rob->programEngine->fData. getPointProList[kk].positionValue[jj];
                    }
            }
            rob->programEngine->fData.cmdStrWriteList=dataList ;
            qDebug()<<"==xx====currentFileName===="<<rob->programEngine->fData.currentFileName<< dataList;
            rob->programEngine->fData.writeProgramFile(  rob->programEngine->fData.currentFileName );
            rob->openFile( rob->programEngine->fData.currentFileName );

           // writeWebCmd( id,  "saveFile:" );
        }
    }else  if( strCmd=="saveXml_" ){
        //
    }else  if( strCmd=="getMsg__" ){
        QString msg;
        returnFlag = 0;
        QVector <Message> allMsgList;
        p_powerManger->messageLog->getAllMessage(allMsgList);
        for(int i=0;i<allMsgList.size(); i++){
            msg.append( QString::number( allMsgList.at(i).messageCode ) );
            msg.append( ";" );
            msg.append( QString::fromStdString( allMsgList.at(i).MessageInformation ) );
            msg.append( "xxx;" );
        }
       // qDebug()<< id<<"xxxxxxx getMsg__"<<msg;
        writeWebCmd( id, QString("getMsg__:" + msg) );
    }else  if( strCmd=="jogStart" ){
        bool isOK=false;
        int axisNumber=dataList.at(0).toInt(&isOK)-1;
        int direction=0;
        if( isOK && dataList.size()==2 ){
            if( dataList.at(1) =="L"){
                direction=0;
            }else{
                direction=1;
            }
            p_powerManger->jogRun( userInfo, axisNumber, direction, returnFlag ) ;
          //  jogRun(soap *, struct UserInfo userInfo,int axisNumber,int direction,int &returnFlag)
        //    jogRun( NULL,userInfo, axisNumber, direction, returnFlag );
        }
    }else if( strCmd=="movZeroP" ){
        p_powerManger->returnZeroPoint( userInfo,  returnFlag ) ;
    }else  if( strCmd=="setZeroP" ){
        p_powerManger->setAllCaliber( userInfo, returnFlag );
    }else  if( strCmd=="setModel" ){
        //T1 T2 A E
        int runmode = ENUM_CONTROL_T1;
        if( strData == "T1"  ){
            runmode = ENUM_CONTROL_T1;
        }else if( strData == "T2"  ){
            runmode = ENUM_CONTROL_T2;
        }else if( strData == "A"  ){
            runmode = ENUM_CONTROL_AUTO;
        }else if( strData == "E"  ){
            runmode = ENUM_CONTROL_EXT;
        }
        p_powerManger->setRunModel( userInfo, runmode, returnFlag ) ;
        //setDebugModel
    }else  if( strCmd=="setSpeed" ){
        if( dataList.size()==2 ){
            int speed= dataList.at(0).toInt();
            if( dataList.at(1) =="T"){
                p_powerManger->setManualSpeedPercent( userInfo, speed, returnFlag ) ;
            }else{
                p_powerManger->setAutoSpeedPercent( userInfo, speed, returnFlag ) ;
            }
        }
    }else  if( strCmd=="login___" ){
        RobotEngine*rob=p_powerManger->robotManager->robotsMap.value( p_powerManger->robotId, NULL );
        if( rob )
        {
            rob->programEngine->fData.currentRobotFileAddress = QString::fromStdString( rob->robotInfo.robotFileAddress );
            qDebug()<<"rob->robotInfo.robotFileAddress=="<< rob->programEngine->fData.currentRobotFileAddress;
        }
        if( dataList.size()==3 ){
            int currentRobotId=dataList.at(2).toUInt();
            p_powerManger->robotId=currentRobotId;
            UpdateDataLv1 updateDataLv1;
            userInfo.password =  dataList.at(1).toStdString();
            if( dataList.at(0)=="admin" ){
                userInfo.account = ENUM_POWER_ADMIN;
            }else{
                userInfo.account = ENUM_POWER_COMMON_CONSUMER;
            }
            p_powerManger->updateControllerInfoLevel1( userInfo, p_powerManger->robotId, updateDataLv1 ) ;

            writeWebCmd( id,  message );
        }
    }else  if( strCmd=="setCoord" ){
        int nCoordId = 0;
        if( strData == "JOINT"  ){
            nCoordId=ENUM_JOINT;
        }else if( strData == "WORLD"  ){
            nCoordId=ENUM_WORLD;
        }else if( strData == "TOOL"  ){
            nCoordId=ENUM_TOOL;
        }else if( strData == "USER"  ){
            nCoordId=ENUM_USER;
        }
        p_powerManger->setCoordMode( userInfo, nCoordId,  returnFlag ) ;
    }else  if( strCmd=="clearMsg" ){
        p_powerManger->setAllMsgClear( userInfo,  returnFlag ) ;
    }else  if( strCmd=="setBrake" ){
        int nServOffOn = ENUM_SERVO_OFF;
        if( strData.indexOf("on")>=0 ){
            nServOffOn=ENUM_SERVO_ON;
        }else{
            nServOffOn=ENUM_SERVO_OFF;
        }
        p_powerManger->setBrake( userInfo,  nServOffOn, returnFlag ) ;
    }

    return 1;
}
